package com.xbts.web.controller.payment;

import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.module.payment.kit.BankBase;
import com.xbts.system.module.payment.enums.PaymentClientEnum;
import com.xbts.system.module.payment.enums.PaymentMethodEnum;
import com.xbts.system.module.payment.kit.CashierSupport;
import com.xbts.system.module.payment.kit.dto.PayParam;
import com.xbts.system.module.payment.kit.params.dto.CashierParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 买家端,收银台接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@RestController
@Api(tags = "买家端,收银台接口")
@RequestMapping("/payment/cashier")
public class CashierController {

    @Autowired
    private CashierSupport cashierSupport;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "client", value = "客户端类型", paramType = "path", allowableValues = "PC,H5,WECHAT_MP,APP", dataTypeClass = String.class)
    })
    @GetMapping(value = "/tradeDetail")
    @ApiOperation(value = "获取支付详情")
    @PreAuthorize("@ss.hasPermi('payment:cashier:query')")
    public AjaxResult paymentParams(@Validated PayParam payParam) {
        CashierParam cashierParam = cashierSupport.cashierParam(payParam);
        return AjaxResult.success(cashierParam);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentMethod", value = "支付方式", paramType = "path", allowableValues = "WECHAT,ALIPAY,BANK_TRANSFER", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentClient", value = "调起方式", paramType = "path", allowableValues = "APP,NATIVE,JSAPI,H5,MP", dataTypeClass = String.class)
    })
    @GetMapping(value = "/pay/{paymentMethod}/{paymentClient}")
    @ApiOperation(value = "支付")
    public AjaxResult payment(
            HttpServletRequest request,
            HttpServletResponse response,
            @PathVariable String paymentMethod,
            @PathVariable String paymentClient,
            @Validated PayParam payParam) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
        PaymentClientEnum paymentClientEnum = PaymentClientEnum.valueOf(paymentClient);
        try {
            return cashierSupport.payment(paymentMethodEnum, paymentClientEnum, request, response, payParam);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;
    }

    /**
     * http://localhost:8555/payment/cashier/pay/bank?orderType=ORDER&sn=CN20230907181538791255838799&clientType=PC
     * 银行卡支付
     * @param request
     * @param response
     * @param payParam
     */
    @GetMapping(value = "/pay/bank")
    @ApiOperation(value = "银行支付")
    public void payment( HttpServletRequest request, HttpServletResponse response, @Validated PayParam payParam) {
        try {
            request.setCharacterEncoding(BankBase.encoding);
            response.setContentType("text/html; charset="+ BankBase.encoding);
            cashierSupport.payment( request, response, payParam);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
    }

    /**
     * 银行支付成功后跳转
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/successRedict", method = RequestMethod.POST)
    public void successRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cashierSupport.successRedirect(request, response);

    }
    @ApiOperation(value = "银行支付回调")
    @RequestMapping(value = "/callback", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult callback(HttpServletRequest request, HttpServletResponse response) {
        cashierSupport.callback(request, response);
        return AjaxResult.success();
    }

    @ApiOperation(value = "支付回调")
    @RequestMapping(value = "/callback/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult callback(HttpServletRequest request, @PathVariable String paymentMethod) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
        cashierSupport.callback(paymentMethodEnum, request);
        return AjaxResult.success();
    }

    @ApiOperation(value = "支付异步通知")
    @RequestMapping(value = "/notify/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public void notify(HttpServletRequest request, @PathVariable String paymentMethod) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
        cashierSupport.notify(paymentMethodEnum, request);
    }

    @ApiOperation(value = "查询支付结果")
    @GetMapping(value = "/result")
    public AjaxResult paymentResult(PayParam payParam) {
        return AjaxResult.success(cashierSupport.paymentResult(payParam));
    }

//    /**
//     * http://localhost:8555/payment/cashier/toPay
//     * 支付接口
//     * @throws IOException
//     */
//    @RequestMapping(value = "toPay",method = RequestMethod.GET)
//    public void toPay(HttpServletRequest request, HttpServletResponse response, BigDecimal amount, String hh) throws IOException {
//        request.setCharacterEncoding(BankBase.encoding);
//        response.setContentType("text/html; charset="+ BankBase.encoding);
//        String html = cashierSupport.toPay(amount);
//        if (StringUtils.isNotEmpty(hh)) {
//            response.getWriter().write(hh);
//        } else {
//            response.getWriter().write(html);
//        }
//    }
}
